﻿/******************************************************************************
* File description: ExpCommonMethod.cpp
* Function: 实验相关公共方法
* Current Version: 1.0.0
* Author: 郑佰旺
* Date: 2025-07-1
*  -----
*  Copyright by 博坤生物 .
 *****************************************************************************/

#include "ExpCommonMethod.h"

extern int npwhfl;
ExpCommonMethod::ExpCommonMethod()
{
}


ExpCommonMethod& ExpCommonMethod::Common()
{
    static ExpCommonMethod instance;
    return instance;
}

bool ExpCommonMethod::checkExpComponent(int module,QString expName)
{
    if(module==0)//全流程
    {
        QSqlDatabase db = dbHelper::openConnection();
        QSqlQuery query(db);
        QString sql = QString("Select ShiftingTargetExp,PurifyingExp From MainExperimentalProcess Where ExperimentName='%1'").arg(expName);
        query.exec(sql);

        QString ReleasseTargetExp,PracticeTargetExp,ShiftingTargetExp,PurifyingExp;
        while (query.next()) {
            ShiftingTargetExp = query.value("ShiftingTargetExp").toString();
            PurifyingExp = query.value("PurifyingExp").toString();
        }

        return checkShiftingTargetComponent(ShiftingTargetExp) && checkPurifyingExpComponent(PurifyingExp);
    }
    else if(module==3)//转靶
    {
        return checkShiftingTargetComponent(expName);
    }
    else if(module==4)//纯化
    {
        return checkPurifyingExpComponent(expName);
    }
    return true;
}

bool ExpCommonMethod::checkShiftingTargetComponent(QString expName){

    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);

    QString sql=QString("Select value From deftab Where id=4 and num=1");
    query.exec(sql);
    int isUsed=0;
    while (query.next()) {
        isUsed=query.value("Value").toInt();
    }
    if(isUsed==0){
        return  true;
    }
    else{
        sql=QString("Select Id From zbexe_config Where Type=0 and value_1='%1'").arg(expName);
        query.exec(sql);
        QString id;
        while (query.next())
        {
            id=query.value("Id").toString();
        }

        int Num=0;
        sql=QString("Select count(*) Num From zbexe_config Where Id='%1' and type='10'").arg(id);
        query.exec(sql);
        while (query.next()) {
            Num=query.value("Num").toInt();
        }
        if(Num>0){
            sql="Select IsUsed From ComponentInfo Where module='3' and Serial='8'";
            query.exec(sql);

            int isUsed=0;

            while (query.next()) {
                isUsed=query.value("IsUsed").toInt();
            }

            return isUsed==1;
        }

        sql=QString("Select count(*) Num From zbexe_config Where Id='%1' and type='10'").arg(id);
        query.exec(sql);
        while (query.next()) {
            Num=query.value("Num").toInt();
        }
        if(Num>0){
            sql="Select IsUsed From ComponentInfo Where module='3' and Serial='9'";
            query.exec(sql);

            int isUsed=0;

            while (query.next()) {
                isUsed=query.value("IsUsed").toInt();
            }

            return isUsed==1;
        }

        return true;
    }

}

bool ExpCommonMethod::checkPurifyingExpComponent(QString expName){

    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Select value From deftab Where id=4 and num=2");
    query.exec(sql);
    int isUsed=0;
    while (query.next()) {
        isUsed=query.value("Value").toInt();
    }
    if(isUsed==0){
        return true;
    }else{
        QString sql=QString("Select Id From exe_config Where Type=0 and value_1='%1'").arg(expName);
        query.exec(sql);
        QString id;
        while (query.next())
        {
            id=query.value("Id").toString();
        }

        int Num=0;
        //盖盖 转盘
        sql=QString("Select count(*) Num From exe_config Where Id='%1' and (type='3' or type='4')").arg(id);
        query.exec(sql);
        while (query.next()) {
            Num=query.value("Num").toInt();
        }
        if(Num>0){
            sql="Select IsUsed From ComponentInfo Where module='4' and Serial='6'";
            query.exec(sql);

            int isUsed=1;

            while (query.next()) {
                isUsed=query.value("IsUsed").toInt();
            }

            if(isUsed!=1){
                return false;
            }
        }

        sql=QString("Select count(*) Num From exe_config Where Id='%1' and type='6'").arg(id);
        query.exec(sql);
        while (query.next()) {
            Num=query.value("Num").toInt();
        }
        if(Num>0){
            sql="Select IsUsed From ComponentInfo Where module='4' and Serial='8'";
            query.exec(sql);

            int isUsed=1;

            while (query.next()) {
                isUsed=query.value("IsUsed").toInt();
            }

            if(isUsed!=1){
                return false;
            }
        }

        sql=QString("Select count(*) Num From exe_config Where Id='%1' and type='11'").arg(id);
        query.exec(sql);
        while (query.next()) {
            Num=query.value("Num").toInt();
        }
        if(Num>0){
            sql="Select IsUsed From ComponentInfo Where module='4' and Serial='9'";
            query.exec(sql);

            int isUsed=1;

            while (query.next()) {
                isUsed=query.value("IsUsed").toInt();
            }

            if(isUsed!=1){
                return false;
            }
        }
        return true;
    }

}

/**
 * @brief 解析总流程实验
 * @param 总实验流程名称
 */
void ExpCommonMethod::analysisTotalProcessExp(QString expName)
{
    step=1;
    unionStep=1;
    unionNumber=1;
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql = QString("Select ReleasseTargetExp,PracticeTargetExp,ShiftingTargetExp,PurifyingExp From MainExperimentalProcess Where ExperimentName='%1'").arg(expName);
    query.exec(sql);

    QString ReleasseTargetExp,PracticeTargetExp,ShiftingTargetExp,PurifyingExp;

    while (query.next()) {
        ReleasseTargetExp = query.value("ReleasseTargetExp").toString();
        PracticeTargetExp = query.value("PracticeTargetExp").toString();
        ShiftingTargetExp = query.value("ShiftingTargetExp").toString();
        PurifyingExp = query.value("PurifyingExp").toString();
    }
    if(StaticProperties::softType==0){
        analysisReleasseTargetExp(ReleasseTargetExp);
        analysisPracticeTargetExp(PracticeTargetExp);
        analysisShiftingTargetExp(ShiftingTargetExp);
        analysisPurifyingExp(PurifyingExp);
    }
    else if(StaticProperties::softType==1){
        analysisReleasseTargetExp(ReleasseTargetExp);
        analysisPracticeTargetExp(PracticeTargetExp);
        analysisShiftingTargetExp(ShiftingTargetExp);
    }
}

/**
 * @brief 解析放靶实验
 * @param 放靶实验名称
 */
void ExpCommonMethod::analysisReleasseTargetExp(QString expName)
{
    if(StaticProperties::currentExpType==1){
        step=1;
    }
    //fbexe_config 0实验信息 1挡板 2取靶 3夹爪 4开盖 5加速端回靶 6转液 7等待 8暂停 9联动
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);

    QString id;
    QString sql;
    sql=QString("Select Id,value_2 From fbexe_config Where value_1='%1' and type='0'").arg(expName);

    //获取实验ID
    query.exec(sql);
    int startExp=1;
    while (query.next()) {
        id=query.value("ID").toString();
        startExp=query.value("value_2").toInt();
    }
    StaticProperties::expStartStepList.append(startExp);
    QList<QVariantMap> exeConfigList;

    sql=QString("Select * From fbexe_config Where id='%1' and Type<>'0' order by Step").arg(id);
    query.exec(sql);
    while (query.next()) {
        QVariantMap map;
        for (int i = 0; i < query.record().count(); ++i) {
            map[query.record().fieldName(i)] = query.value(i);
        }
        exeConfigList.append(map);
    }
    for(int i=0;i<exeConfigList.length();i++){
        int type=exeConfigList[i].value("type").toInt();
        switch (type) {
        case 1://挡板
            analysisBarrier(1,exeConfigList[i]);
            break;
        case 2://取靶
            analysisAcquisitionTarget(1,exeConfigList[i]);
            break;
        case 3://夹爪
            analysisClampingJaw(1,exeConfigList[i]);
            break;
        case 5://传靶
            analysisTransmissionTarget(1,exeConfigList[i]);
            break;
        case 7://等待
            analysisWait(1,exeConfigList[i]);
            break;
        case 8://暂停
            analysisPause(1,exeConfigList[i]);
            break;
        case 9://联动
            analysisLinkage(1,exeConfigList[i]);
            break;
        }
    }
}

/**
 * @brief 解析打靶实验
 * @param 打靶实验名称
 */
void ExpCommonMethod::analysisPracticeTargetExp(QString expName)
{
    if(StaticProperties::currentExpType==2){
        step=1;
    }
    //dbexe_config 0实验信息 1吸取梭体 2冷却 3干燥 4锁体组合运行 5抱闸状态 6进料 7加速端回靶 8等待 9暂停 10联动
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);

    QString id;
    QString sql;
    sql=QString("Select Id,value_2 From dbexe_config Where value_1='%1' and type='0'").arg(expName);

    //获取实验ID
    query.exec(sql);
    int startStep=1;
    while (query.next()) {
        id=query.value("ID").toString();
        startStep=query.value("value_2").toInt();
    }
    StaticProperties::expStartStepList.append(startStep);

    QList<QVariantMap> exeConfigList;

    sql=QString("Select * From dbexe_Config Where id='%1' and Type<>'0' order by Step").arg(id);
    query.exec(sql);
    while (query.next()) {
        QVariantMap map;
        for (int i = 0; i < query.record().count(); ++i) {
            map[query.record().fieldName(i)] = query.value(i);
        }
        exeConfigList.append(map);
    }
    for(int i=0;i<exeConfigList.length();i++){
        int type=exeConfigList[i].value("type").toInt();
        switch (type) {
        case 1://吸取梭体
            analysisSuctionTarget(2,exeConfigList[i]);
            break;
        case 2://冷却
            analysisCooling(2,exeConfigList[i]);
            break;
        case 3://干燥
            analysisDry(2,exeConfigList[i]);
            break;
        case 4://锁体组合运行
            analysisMoveTarget(2,exeConfigList[i]);
            break;
        case 5://锁死
            analysisLockedUp(2,exeConfigList[i]);
            break;
        case 6://进料
            analysisFeed(2,exeConfigList[i]);
            break;
        case 7://传靶
            analysisTransmissionTarget(2,exeConfigList[i]);
            break;
        case 8://等待
            analysisWait(2,exeConfigList[i]);
            break;
        case 9://暂停
            analysisPause(2,exeConfigList[i]);
            break;
        case 10://联动
            analysisLinkage(2,exeConfigList[i]);
            break;
        }
    }
}

/**
 * @brief 解析转靶实验
 * @param 转靶实验
 */
void ExpCommonMethod::analysisShiftingTargetExp(QString expName)
{
    if(StaticProperties::currentExpType==3){
        step=1;
    }
    //zbexe_config 0实验信息 1挡板 2取靶 3夹爪 4开盖 5加速端回靶 6转液 7等待 8暂停 9联动 10活性检测
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);

    QString id;
    QString sql;
    sql=QString("Select Id,value_2 From zbexe_config Where value_1='%1' and type='0'").arg(expName);

    //获取实验ID
    query.exec(sql);

    int startStep=1;
    while (query.next()) {
        id=query.value("ID").toString();
        startStep=query.value("value_2").toInt();
    }
    StaticProperties::expStartStepList.append(startStep);

    QList<QVariantMap> exeConfigList;

    sql=QString("Select * From zbexe_config Where id='%1' and Type<>'0' order by Step").arg(id);
    query.exec(sql);
    while (query.next()) {
        QVariantMap map;
        for (int i = 0; i < query.record().count(); ++i) {
            map[query.record().fieldName(i)] = query.value(i);
        }
        exeConfigList.append(map);
    }
    for(int i=0;i<exeConfigList.length();i++){
        int type=exeConfigList[i].value("type").toInt();

        switch (type) {
        case 1://挡板
            analysisBarrier(3,exeConfigList[i]);
            break;
        case 2://取靶
            analysisAcquisitionTarget(3,exeConfigList[i]);
            break;
        case 3://夹爪
            analysisClampingJaw(3,exeConfigList[i]);
            break;
        case 4://开盖
            analysisOpenTheLid(3,exeConfigList[i]);
            break;
        case 5://传靶
            analysisTransmissionTarget(3,exeConfigList[i]);
            break;
        case 6://转液
            analysisFluidTransfer(3,exeConfigList[i]);
            break;
        case 7://等待
            analysisWait(3,exeConfigList[i]);
            break;
        case 8://暂停
            analysisPause(3,exeConfigList[i]);
            break;
        case 9://联动
            analysisLinkage(3,exeConfigList[i]);
            break;
        case 10://活检
            analysisActivityTesting(3,exeConfigList[i]);
            break;
        case 11://旋盖夹爪
            analysisRotaryCapClamp(3,exeConfigList[i]);
            break;
        }
    }
}

/**
 * @brief 解析纯化实验(包含循环)
 * @param 纯化实验
 */
void ExpCommonMethod::analysisPurifyingExp(QString expName,bool isUnion)
{
    if(StaticProperties::currentExpType==4){
        step=1;
    }
    //exe_config 0实验信息 1注射器 2阀门 3转盘 4等待 5检测器 6循环 7跳过 8盖盖 9气泵
    bool isCircle=false;
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);

    QString id;
    QString code;
    QString sql=QString("Select Id,value_2,value_4 From exe_config Where value_1='%1' and value_2='%2' and type='0'").arg(expName,StaticProperties::currentExpConsumables);
    //获取实验ID
    query.exec(sql);
    int startStep=1;

    while (query.next()) {
        id=query.value("ID").toString();
        code=query.value("value_2").toString();
        startStep=query.value("value_4").toInt();
    }
    StaticProperties::expStartStepList.append(startStep);

    QMap<QString,QVariantMap> circleStepList;

    sql=QString("Select * From exe_config Where Id='%1' and type='8' order by Step ").arg(id);
    query.exec(sql);
    while (query.next())
    {
        QVariantMap map;
        map["Begin"]=query.value("value_1").toString();
        map["End"]=query.value("value_2").toString();
        map["CircleNum"]=query.value("value_3").toString();
        map["Step"]=query.value("step").toString();
        circleStepList.insert(query.value("value_1").toString(),map);
    }

    QList<QVariantMap> exeConfigList;

    sql=QString("Select * From exe_Config Where id='%1' and Type<>'0' order by Step").arg(id);
    query.exec(sql);
    while (query.next()) {
        QVariantMap map;
        for (int i = 0; i < query.record().count(); ++i) {
            map[query.record().fieldName(i)] = query.value(i);
        }
        exeConfigList.append(map);
    }
    for(int i=0;i<exeConfigList.length();i++){
        if(circleStepList.contains(exeConfigList[i].value("step").toString()))
        {
            isCircle=true;
            QVariantMap beginMap;
            beginMap["Type"]="1";//0 正常执行(Info发送指令) 1循环(开始循环,结束循环) 2 跳过("") 3等待(等待时长)
            beginMap["Step"]=circleStepList[exeConfigList[i].value("step").toString()]["Step"].toString();
            beginMap["Info"]="开始循环";
            StaticProperties::expSendList.insert(step,beginMap);
            step++;
            QString startNum=circleStepList[exeConfigList[i].value("step").toString()]["Begin"].toString();
            QString endNum= circleStepList[exeConfigList[i].value("step").toString()]["End"].toString();
            int circleNum= circleStepList[exeConfigList[i].value("step").toString()]["CircleNum"].toInt();

            for(int a=0;a<circleNum;a++){
                for(int m=startNum.toInt();m<=endNum.toInt();m++)
                {
                    int type=exeConfigList[m-1].value("type").toInt();
                    switch (type) {
                    case 1://注射器
                        analysisSyringe(4,exeConfigList[m-1],code,"0",isCircle,isUnion);
                        break;
                    case 2://阀门
                        analysisValve(4,exeConfigList[m-1],isCircle,isUnion);
                        break;
                    case 3://转盘
                        analysisTurntable(4,exeConfigList[m-1],isUnion);
                        break;
                    case 4://盖盖
                        analysisCoverTheLid(4,exeConfigList[m-1],isUnion);
                        break;
                    case 6://转液
                        analysisFluidTransfer(4,exeConfigList[m-1],isUnion);
                        break;
                    case 7://等待
                        analysisWait(4,exeConfigList[m-1],isCircle,isUnion);
                        break;
                    case 9://跳过
                        analysisSkip(4,exeConfigList[m-1]);
                        m=exeConfigList[m-1].value("Step").toInt()+exeConfigList[m-1].value("Value_1").toInt();                        break;
                    case 10://暂停
                        analysisPause(4,exeConfigList[m-1]);
                        break;
                    case 11://加热
                        analysisHeating(4,exeConfigList[m-1],isUnion);
                        break;
                    }
                }

                QVariantMap map;
                map["Type"]="1";//0 正常执行(Info发送指令) 1循环(开始循环,结束循环) 2 跳过("") 3等待(等待时长)
                map["Step"]=circleStepList[exeConfigList[i].value("step").toString()]["Step"].toString();
                map["Info"]=a+1==circleNum?"完成":QString("第%1次循环完成").arg(QString::number(a+1));
                StaticProperties::expSendList.insert(step,map);
                step++;
            }
            i=circleStepList[exeConfigList[i].value("step").toString()]["Step"].toInt()-1;
        }
        else {
            int type=exeConfigList[i].value("type").toInt();
            QString testType="0";

            switch (type) {
            case 1://注射器
                if(exeConfigList.length()>i+1){
                    if(exeConfigList[i+1].value("type").toInt()==5)
                    {
                        testType="7";
                    }
                }
                analysisSyringe(4,exeConfigList[i],code,testType,isCircle,isUnion);
                break;
            case 2://阀门
                analysisValve(4,exeConfigList[i],isCircle,isUnion);
                break;
            case 3://转盘
                analysisTurntable(4,exeConfigList[i],isUnion);
                break;
            case 4://盖盖
                analysisCoverTheLid(4,exeConfigList[i],isUnion);
                break;
            case 5://检测器
                analysisProductTesting(4,exeConfigList[i]);
                break;
            case 6://转液
                analysisFluidTransfer(4,exeConfigList[i],isUnion);
                break;
            case 7://等待
                analysisWait(4,exeConfigList[i],isCircle,isUnion);
                break;
            case 9://跳过
                analysisSkip(4,exeConfigList[i]);
                i=exeConfigList[i].value("step").toInt()+exeConfigList[i].value("value_1").toInt()-1;
                break;
            case 10://暂停
                analysisPause(4,exeConfigList[i]);
                break;
            case 11://加热
                analysisHeating(4,exeConfigList[i],isUnion);
                break;
            }
        }
    }
}

/**
 * @brief 实验前校验启动条件
 * @return 校验结果,空为可以启动实验
 */
QString ExpCommonMethod::checkExpRunningRule(){
    if(npwhfl!=0){
        return "正在读取能谱数据,无法启动实验)";
    }
    //    bool resetResult=checkResetStatus();
    //    if(!resetResult){
    //        return "请先进行复位后,再启动实验";
    //    }
    bool checkResult= ExpCommonMethod::Common().checkExpComponent(StaticProperties::currentExpType,StaticProperties::currentExpName);
    if(!checkResult){
        return  "实验配置中存在屏蔽的组件,禁止启动实验";
    }
    if((StaticProperties::currentExpType==0 && StaticProperties::softType==0) || StaticProperties::currentExpType==4)
    {
        QSqlDatabase db = dbHelper::openConnection();
        QSqlQuery query(db);
        QString sql="Select IsBlocked From DetectorInformation Where Serial='2'";
        query.exec(sql);
        bool isBlocked=false;
        while (query.next()) {
            isBlocked=query.value("IsBlocked")=="否";
        }
        if(StaticProperties::purifyingStatus.value("2")=="无" && isBlocked)
        {
            return "未检测到放置耗材,无法启动实验";
        }

        sql="Select IsBlocked From DetectorInformation Where Serial='1'";
        query.exec(sql);
        while (query.next()) {
            isBlocked=query.value("IsBlocked")=="否";
        }

        if(isBlocked &&(StaticProperties::purifyingStatus.value("0")=="松开" || StaticProperties::purifyingStatus.value("1")=="松开")){
            return "耗材未压紧,无法启动实验";
        }

        QString expName=StaticProperties::currentExpName;
        if(StaticProperties::currentExpType==0){
            sql=QString("Select PurifyingExp From MainExperimentalProcess Where ExperimentName='%1'").arg(expName);
            query.exec(sql);
            while (query.next()) {
                expName=query.value("PurifyingExp").toString();
            }
        }
        sql=QString("Select value_2 From exe_config Where type=0 and value_1='%1'").arg(expName);
        query.exec(sql);
        QString code;
        while (query.next()) {
            code=query.value("value_2").toString();
        }

        return checkConsumablesSyring(CommonHelper::Common().getItemName("3",code));
    }
    if (StaticProperties::currentExpType==3 || StaticProperties::softType==1){
        QSqlDatabase db = dbHelper::openConnection();
        QSqlQuery query(db);
        QString expName=StaticProperties::currentExpName;
        QString sql;
        if(StaticProperties::currentExpType==0){
            sql=QString("Select ShiftingTargetExp From MainExperimentalProcess Where ExperimentName='%1'").arg(expName);
            query.exec(sql);
            while (query.next()) {
                expName=query.value("ShiftingTargetExp").toString();
            }
        }
        sql=QString("Select count(*) as Num From zbexe_config Where type='9' and ID=(Select Id From zbexe_config Where type='0' and value_1='%1')").arg(expName);
        query.exec(sql);
        while (query.next()) {
            return query.value("Num").toInt()>0?"有联动不允许运行":"";
        }
    }
    if(StaticProperties::currentExpType==2 || StaticProperties::softType==1){
        QSqlDatabase db = dbHelper::openConnection();
        QSqlQuery query(db);
        QString expName=StaticProperties::currentExpName;
        QString sql;
        if(StaticProperties::currentExpType==0){
            sql=QString("Select PracticeTargetExp From MainExperimentalProcess Where ExperimentName='%1'").arg(expName);
            query.exec(sql);
            while (query.next()) {
                expName=query.value("PracticeTargetExp").toString();
            }
        }
        sql=QString("Select count(*) as Num From dbexe_config Where type='10' and ID=(Select Id From dbexe_config Where type='0' and value_1='%1')").arg(expName);
        query.exec(sql);
        while (query.next()) {
            return query.value("Num").toInt()>0?"有联动不允许运行":"";
        }
    }
    return  "";
}

bool ExpCommonMethod::checkResetStatus()
{
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    if(StaticProperties::currentExpType==0){
        if(StaticProperties::softType==0){
            QString sql="Select count(*) num From ModuleResetStatus Where status<>1";
            query.exec(sql);
            while (query.next()) {
                return query.value("num").toInt()==0;
            }
        }
        else if(StaticProperties::softType==1){
            QString sql="Select count(*) num From ModuleResetStatus Where Module<>4 and status<>1";
            query.exec(sql);
            while (query.next()) {
                return query.value("num").toInt()==0;
            }
        }
    }
    else{
        QString sql=QString("Select status From ModuleResetStatus Where module='%1'").arg(StaticProperties::currentExpType==3?"1":QString::number(StaticProperties::currentExpType));
        query.exec(sql);
        while (query.next()) {
            return query.value("status")==1;
        }
    }
    return true;
}

/**
 * @brief 校验耗材注射器信息
 * @param 耗材名称
 * @return
 */
QString ExpCommonMethod::checkConsumablesSyring(QString name)
{
    QString code= CommonHelper::Common().getItemCode("3",name);
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);

    QString sql=QString("Select count(*) As Num From SyringeMaintenance Where ConsumablesTypes='%1'").arg(code);
    query.exec(sql);
    int result=0;
    while (query.next())
    {
        result=query.value("Num").toInt();
    }

    if(result==0){
        return "当前耗材注射器参数配置不全";
    }
    else{
        sql=QString("Select count(*) As Num From SyringeMaintenance Where ConsumablesTypes='%1' and (InsideDiameter is ''  or InitialPosition  is '' or ReductionRatio is '' or ScrewLead is '' or CircleStep is '' )").arg(code);
        query.exec(sql);
        while (query.next())
        {
            result=query.value("Num").toInt();
        }
        if(result>0){
            return  "当前耗材注射器参数配置不全";
        }
    }
    return  "";

}

/** 保存实验信息
 * @brief
 */
void ExpCommonMethod::saveExpInfo(){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);

    QString sql=QString("Insert into ExperimentInfo(ExpModule,ExpName,ExpStep) Values('%1','%2','%3')").arg(QString::number(StaticProperties::currentExpType),StaticProperties::currentExpName,"1");
    query.exec(sql);

    db.transaction();
    for (auto outerIt = StaticProperties::expSendList.constBegin(); outerIt != StaticProperties::expSendList.constEnd(); ++outerIt) {
        const QVariantMap &innerMap = outerIt.value();
        sql=QString("Insert into ExperimentDetail Values('%1','%2','%3','%4','%5','')").arg(
                    QString::number(outerIt.key()),innerMap["Type"].toString(),innerMap["Module"].toString(),
                innerMap["Step"].toString(),innerMap["Info"].toString());
        query.exec(sql);
    }
    for (auto outerIt = StaticProperties::expConcurrencySendList.constBegin(); outerIt != StaticProperties::expConcurrencySendList.constEnd(); ++outerIt) {
        const QVariantMap &innerMap = outerIt.value();
        sql=QString("Insert into ExperimentDetail Values('%1','%2','%3','%4','%5','%6')").arg(
                    QString::number(outerIt.key()),innerMap["Type"].toString(),innerMap["Module"].toString(),
                QString::number(innerMap["Step"].toInt()+500),innerMap["Info"].toString(),innerMap["UnionId"].toString());
        qDebug()<<sql;
        query.exec(sql);
    }
    db.commit();
}

/** 转液
 * @brief ExpCommonMethod::analysisFluidTransfer
 * @param module 模块
 * @param map 信息
 */
void ExpCommonMethod::analysisFluidTransfer(int module,QVariantMap map,bool isUnion)
{
    QVariantMap result;
    result["Type"]="0";
    result["Module"]=module;
    result["Step"]=step;
    result["Info"]= CommCommand::I().getFluidTransfer(module,QString::number(step),map.value("value_1").toString(),map.value("value_2").toString(),map.value("value_3").toString());
    StaticProperties::totalExpTime+=map.value("value_3").toDouble();

    if(isUnion){

    }
    else{
        StaticProperties::expSendList.insert(step,result);
        step++;
    }
}

/**
 * @brief ExpCommonMethod::analysisBarrier 挡板
 * @param module 模块
 * @param map 信息
 */
void ExpCommonMethod::analysisBarrier(int module,QVariantMap map)
{
    QVariantMap result;
    result["Type"]="0";
    result["Module"]=module;
    result["Step"]=step;
    result["Info"]= CommCommand::I().getBarrier(QString::number(step),map.value("value_1").toString());
    StaticProperties::totalExpTime+=expTimeXml.getBarrier();

    StaticProperties::expSendList.insert(step,result);
    step++;
}

/**
 * @brief ExpCommonMethod::analysisTransmissionTarget 传靶
 * @param module
 * @param map
 */
void ExpCommonMethod::analysisTransmissionTarget(int module,QVariantMap map)
{
    QVariantMap result;
    result["Type"]="0";
    result["Module"]=module;
    result["Step"]=step;
    result["Info"]= CommCommand::I().getTransmissionTarget(module,QString::number(step),map.value("value_1").toString());
    StaticProperties::totalExpTime+=expTimeXml.getTransmissionTarget(module);

    StaticProperties::expSendList.insert(step,result);
    step++;
}

/**
 * @brief ExpCommonMethod::analysisSuctionTarget 吸取梭体
 * @param module
 * @param map
 */
void ExpCommonMethod::analysisSuctionTarget(int module,QVariantMap map)
{
    QVariantMap result;
    result["Type"]="0";
    result["Module"]=module;
    result["Step"]=step;
    result["Info"]= CommCommand::I().getSuctionTarget(QString::number(step),map.value("value_1").toString());
    StaticProperties::totalExpTime+=expTimeXml.getSuctionTarget();

    StaticProperties::expSendList.insert(step,result);
    step++;
}

/**
 * @brief ExpCommonMethod::analysisCooling 冷却
 * @param module
 * @param map
 */
void ExpCommonMethod::analysisCooling(int module,QVariantMap map)
{
    QVariantMap result;
    result["Type"]="0";
    result["Module"]=module;
    result["Step"]=step;
    result["Info"]= CommCommand::I().getCooling(QString::number(step),map.value("value_1").toString(),map.value("value_2").toString());
    StaticProperties::totalExpTime+=map.value("value_1").toInt()*60;

    StaticProperties::expSendList.insert(step,result);
    step++;
}

/**
 * @brief ExpCommonMethod::analysisDry 干燥
 * @param module
 * @param map
 */
void ExpCommonMethod::analysisDry(int module,QVariantMap map){
    QVariantMap result;
    result["Type"]="0";
    result["Module"]=module;
    result["Step"]=step;
    result["Info"]= CommCommand::I().getDry(QString::number(step),map.value("value_1").toString());
    StaticProperties::totalExpTime+=map.value("value_1").toInt()*60;

    StaticProperties::expSendList.insert(step,result);
    step++;
}

/**
 * @brief ExpCommonMethod::analysisMoveTarget 锁体组合运行
 * @param module
 * @param map
 */
void ExpCommonMethod::analysisMoveTarget(int module,QVariantMap map){
    QVariantMap result;
    result["Type"]="0";
    result["Module"]=module;
    result["Step"]=step;
    result["Info"]= CommCommand::I().getMoveTarget(QString::number(step),map.value("value_1").toString());
    StaticProperties::totalExpTime+=expTimeXml.getMoveTarget();

    StaticProperties::expSendList.insert(step,result);
    step++;
}

/**
 * @brief ExpCommonMethod::analysisLockedUp 抱闸
 * @param module
 * @param map
 */
void ExpCommonMethod::analysisLockedUp(int module,QVariantMap map){
    QVariantMap result;
    result["Type"]="0";
    result["Module"]=module;
    result["Step"]=step;
    result["Info"]= CommCommand::I().getLockedUp(QString::number(step),map.value("value_1").toString());
    StaticProperties::totalExpTime+=expTimeXml.getLockedUp();

    StaticProperties::expSendList.insert(step,result);
    step++;
}

/**
 * @brief ExpCommonMethod::analysisFeed 梭体上靶
 * @param module
 * @param map
 */
void ExpCommonMethod::analysisFeed(int module,QVariantMap map){
    QVariantMap result;
    result["Type"]="0";
    result["Module"]=module;
    result["Step"]=step;
    result["Info"]= CommCommand::I().getFeed(QString::number(step),map.value("value_1").toString());
    StaticProperties::totalExpTime+=expTimeXml.getFeed();

    StaticProperties::expSendList.insert(step,result);
    step++;
}

/**
 * @brief ExpCommonMethod::analysisOpenTheLid 开盖
 * @param module
 * @param map
 */
void ExpCommonMethod::analysisOpenTheLid(int module,QVariantMap map)
{
    QVariantMap result;
    result["Type"]="0";
    result["Module"]=module;
    result["Step"]=step;
    result["Info"]= CommCommand::I().getOpenTheLid(QString::number(step),map.value("value_1").toString());
    StaticProperties::totalExpTime+=expTimeXml.getOpenTheLid();

    StaticProperties::expSendList.insert(step,result);
    step++;
}

/**
 * @brief ExpCommonMethod::analysisOpenTheLid 旋盖夹爪
 * @param module
 * @param map
 */
void ExpCommonMethod::analysisRotaryCapClamp(int module,QVariantMap map)
{
    QVariantMap result;
    result["Type"]="0";
    result["Module"]=module;
    result["Step"]=step;
    result["Info"]= CommCommand::I().rotaryCapClamp(QString::number(step),map.value("value_1").toString(),map.value("value_2").toString());
    StaticProperties::totalExpTime+=expTimeXml.getRotaryCapClamp();

    StaticProperties::expSendList.insert(step,result);
    step++;
}

/**
 * @brief ExpCommonMethod::analysisAcquisitionTarget 取放靶
 * @param module
 * @param map
 */
void ExpCommonMethod::analysisAcquisitionTarget(int module,QVariantMap map)
{
    QVariantMap result;
    result["Type"]="0";
    result["Module"]=module;
    result["Step"]=step;
    result["Info"]= CommCommand::I().getAcquisitionTarget(QString::number(step),map.value("value_1").toString(),map.value("value_2").toString(),map.value("value_3").toString());
    StaticProperties::totalExpTime+=expTimeXml.getAcquisitionTarget();

    StaticProperties::expSendList.insert(step,result);
    step++;
}

/**
 * @brief ExpCommonMethod::analysisClampingJaw 夹爪
 * @param module
 * @param map
 */
void ExpCommonMethod::analysisClampingJaw(int module,QVariantMap map)
{
    QVariantMap result;
    result["Type"]="0";
    result["Module"]=module;
    result["Step"]=step;
    result["Info"]= CommCommand::I().getClampingJaw(QString::number(step),map.value("value_1").toString());
    StaticProperties::totalExpTime+=expTimeXml.getClampingJaw();

    StaticProperties::expSendList.insert(step,result);
    step++;
}


/** 检测器
 * @brief ExpCommonMethod::analysisProductTesting
 * @param module 模块
 * @param map 信息
 */
void ExpCommonMethod::analysisProductTesting(int module,QVariantMap map)
{
    QVariantMap result;

    result["Type"]="4";
    result["Module"]=module;
    result["Step"]=step;
    if(map.value("value_4").toString()=="手动"){
        result["Info"]="手动";
    }
    else{
        result["Info"]=map.value("value_1").toString()+","+map.value("value_2").toString();
    }

    StaticProperties::expSendList.insert(step,result);
    step++;
}

/** 注射器
 * @brief ExpCommonMethod::analysisSyringe
 * @param module 模块
 * @param map 信息
 * @param code 耗材编码
 * @param type 类型
 * @param isCircle 是否循环
 */
void ExpCommonMethod::analysisSyringe(int module,QVariantMap map,QString code,QString type,bool isCircle,bool isUnion)
{
    QVariantMap result;

    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);

    QString sql=QString("Select InsideDiameter,ReductionRatio,ScrewLead,CircleStep From SyringeMaintenance Where ConsumablesTypes='%1' and Id='%2'").arg(code,map.value("value_1").toString());
    query.exec(sql);
    double InsideDiameter,ScrewLead,CircleStep=1;
    QString ReductionRatio;
    while (query.next()) {
        InsideDiameter=query.value("InsideDiameter").toDouble();
        ReductionRatio=query.value("ReductionRatio").toString();
        ScrewLead=query.value("ScrewLead").toDouble();
        CircleStep=query.value("CircleStep").toDouble();
    }
    double speed=map.value("value_8").toDouble();

    // 速度单位:ml/分
    if(map.value("value_9")=="mL/Min")
    {
        speed=CommonHelper::Common().minToSecond(QString::number(speed),QString::number(InsideDiameter)).toDouble();
    }

    QStringList reList=ReductionRatio.split(':');

    double stepNum=map.value("value_6").toDouble()/ScrewLead*CircleStep*reList[1].toDouble();
    double time=map.value("value_6").toDouble()/speed;
    double stepNumber = stepNum/time;

    result["Type"]=type;//0 正常执行(Info发送指令) 1循环(开始循环,结束循环) 2 跳过("") 3等待(等待时长) 4检测器 5联动 6检测注射器
    result["Module"]=module;
    result["Step"]=isUnion?unionStep:step;

    StaticProperties::totalExpTime+=time;

    if(isUnion){
        result["Info"]=CommCommand::I().getSyring(QString::number(1),map.value("value_1").toString(),map.value("value_4").toString(),QString::number(stepNumber),QString::number(stepNum));
        result["UnionId"]=unionNumber;
        StaticProperties::expConcurrencySendList.insert(unionStep,result);
        unionStep++;
    }
    else{
        result["Info"]=CommCommand::I().getSyring(QString::number(step),map.value("value_1").toString(),map.value("value_4").toString(),QString::number(stepNumber),QString::number(stepNum));
        StaticProperties::expSendList.insert(step,result);
        step++;
    }
}

/** 阀门
 * @brief ExpCommonMethod::analysisValve
 * @param module 模块
 * @param map 信息
 * @param isCircle 是否循环
 */
void ExpCommonMethod::analysisValve(int module,QVariantMap map,bool isCircle,bool isUnion)
{
    QVariantMap result;
    result["Type"]="0";
    result["Module"]=module;
    result["Step"]=isUnion?unionStep:step;

    StaticProperties::totalExpTime+=expTimeXml.getValve();

    if(isUnion){
        result["Info"]= CommCommand::I().getValve("1",map.value("value_1").toString(),map.value("value_2").toString());
        result["UnionId"]=unionNumber;

        StaticProperties::expConcurrencySendList.insert(unionStep,result);
        unionStep++;
    }
    else{
        result["Info"]= CommCommand::I().getValve(QString::number(step),map.value("value_1").toString(),map.value("value_2").toString());

        StaticProperties::expSendList.insert(step,result);
        step++;
    }
}

/** 活性检测
 * @brief ExpCommonMethod::analysisActivityTesting
 * @param module
 * @param map
 */
void ExpCommonMethod::analysisActivityTesting(int module,QVariantMap map)
{
    QVariantMap result;
    result["Type"]="0";
    result["Module"]=module;
    result["Step"]=step;
    result["Info"]= CommCommand::I().getActivityTesting(QString::number(step),map.value("value_1").toString());
    StaticProperties::totalExpTime+=expTimeXml.getActivityTesting();

    StaticProperties::expSendList.insert(step,result);
    step++;
}

/** 解析加热
 * @brief ExpCommonMethod::analysisHeating
 * @param module
 * @param map
 */
void ExpCommonMethod::analysisHeating(int module,QVariantMap map,bool isUnion)
{
    QVariantMap result;
    result["Type"]="0";
    result["Module"]=module;
    result["Step"]=isUnion?unionStep:step;

    StaticProperties::totalExpTime+=map.value("value_2").toDouble();

    if(isUnion){
        result["Info"]= CommCommand::I().getHeating(QString::number(1),map.value("value_1").toString(),map.value("value_2").toString());
        result["UnionId"]=unionNumber;
        StaticProperties::expConcurrencySendList.insert(unionStep,result);
        unionStep++;
    }
    else{
        result["Info"]= CommCommand::I().getHeating(QString::number(step),map.value("value_1").toString(),map.value("value_2").toString());
        StaticProperties::expSendList.insert(step,result);
        step++;
    }
}

/** 盖盖
 * @brief ExpCommonMethod::analysisCoverTheLid
 * @param module
 * @param map
 */
void ExpCommonMethod::analysisCoverTheLid(int module,QVariantMap map,bool isUnion){
    if(map.value("value_1").toString()=="是"){
        QVariantMap result;
        result["Type"]="0";
        result["Module"]=module;
        result["Step"]=isUnion?unionStep:step;
        StaticProperties::totalExpTime+=expTimeXml.getCoverTheLid();

        if(isUnion){
            result["Info"]= CommCommand::I().coverTheLid(QString::number(1));
            result["UnionId"]=unionNumber;
            StaticProperties::expConcurrencySendList.insert(unionStep,result);
            unionStep++;
        }
        else{
            result["Info"]= CommCommand::I().coverTheLid(QString::number(step));
            StaticProperties::expSendList.insert(step,result);
            step++;
        }
    }
}

/** 转盘
 * @brief ExpCommonMethod::analysisTurntable
 * @param module
 * @param map
 */
void ExpCommonMethod::analysisTurntable(int module,QVariantMap map,bool isUnion){
    QVariantMap result;
    result["Type"]="0";
    result["Module"]=module;
    result["Step"]=isUnion?unionStep:step;
    StaticProperties::totalExpTime+=expTimeXml.getActivityTesting();
    if(isUnion){
        result["Info"]= CommCommand::I().getTurntable(QString::number(1),map.value("value_1").toString());
        result["UnionId"]=unionNumber;
        StaticProperties::expConcurrencySendList.insert(unionStep,result);
        unionStep++;
    }
    else{
        result["Info"]= CommCommand::I().getTurntable(QString::number(step),map.value("value_1").toString());
        StaticProperties::expSendList.insert(step,result);
        step++;
    }
}

//公共方法

/**
 * @brief ExpCommonMethod::analysisSkip 跳过
 * @param module
 * @param map
 */
void ExpCommonMethod::analysisSkip(int module,QVariantMap map){
    for(int n=0;n<=map.value("value_1").toInt();n++)
    {
        QVariantMap result;
        result["Type"]="2";//0 正常执行(Info发送指令) 1循环(开始循环,结束循环) 2 跳过("") 3等待(等待时长)
        result["Module"]=module;
        result["Step"]=step;
        result["Info"]="";
        StaticProperties::expSendList.insert(step,result);
        step++;
    }
}

/**
 * @brief ExpCommonMethod::analysisWait 等待
 * @param module
 * @param map
 * @param isCircle
 */
void ExpCommonMethod::analysisWait(int module,QVariantMap map,bool isCircle,bool isUnion){
    QVariantMap result;

    result["Type"]="3";
    result["Module"]=module;
    //    result["Step"]=isCircle?map.value("step").toInt():step;
    result["Step"]=isUnion?unionStep:step;
    result["WaitType"]=map.value("value_1").toString();
    if(map.value("value_1").toString()=="1"){
        result["Info"]=QString::number(step-(map.value("step").toInt()-map.value("value_3").toInt()));
    }
    else{
        result["Info"]=map.value("value_2").toString();
        StaticProperties::totalExpTime+=map.value("value_2").toDouble();
    }

    if(isUnion){
        result["UnionId"]=unionNumber;
        StaticProperties::expConcurrencySendList.insert(unionStep,result);
        unionStep++;
    }
    else{
        StaticProperties::expSendList.insert(step,result);
        step++;
    }
}

/**
 * @brief ExpCommonMethod::analysisPause 暂停
 * @param module
 * @param map
 */
void ExpCommonMethod::analysisPause(int module,QVariantMap map)
{
    QVariantMap result;
    result["Type"]="6";
    result["Module"]=module;
    result["Step"]=step;
    result["Info"]=map.value("value_2").toString();

    StaticProperties::expSendList.insert(step,result);
    step++;
}

/**
 * @brief ExpCommonMethod::analysisLinkage 联动
 * @param module
 * @param map
 */
void ExpCommonMethod::analysisLinkage(int module,QVariantMap map)
{
    //同步执行
    if(map.value("value_4")=="是"){
        QVariantMap result;
        result["Type"]="8";
        result["Module"]=module;
        result["Step"]=step;
        result["UnionId"]=unionNumber;
        StaticProperties::expSendList.insert(step,result);
        step++;

        QString linkModule=map.value("value_1").toString();
        if(linkModule=="纯化")
        {
            analysisPurifyingExp(map.value("value_3").toString(),true);
        }
        unionNumber++;
    }
    else
    {
        QVariantMap result;
        result["Type"]="5";
        result["Module"]=module;
        result["Step"]=step;

        StaticProperties::expSendList.insert(step,result);
        step++;

        QString linkModule=map.value("value_1").toString();
        if(linkModule=="纯化")
        {
            analysisPurifyingExp(map.value("value_3").toString());
        }
    }

}


